package com.petlab.forum.source;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.petlab.model.Article;
import com.petlab.model.Comment;

public class ReadInformationFromXMLFile {

	private final static int pagesize = 10;

	public static HashMap<String, List<?>> getInformation(String filename,
			int articlePage) {
		SAXReader saxReader = new SAXReader();
		HashMap<String, List<?>> hashMap = new HashMap<String, List<?>>();

		List<Article> articles = new ArrayList<Article>();
		List<Comment> comments = new ArrayList<Comment>();

		try {
			Document document = saxReader.read(filename);
			Element root = document.getRootElement();
			List<?> elements = root.elements();

			Element articleElement = (Element) elements.get(0);
			Article article = new Article();
			int articleNum = Integer.parseInt(articleElement
					.attributeValue("articleId"));
			String themeTitle = articleElement.elementText("themeTitle");
			String articleTitle = articleElement.elementText("artilceTitle");
			String authorName = articleElement.elementText("authorName");
			String articleContent = articleElement
					.elementText("articleContent");
			String inTime = articleElement.elementText("inTime");

			article.setArticleId(articleNum);
			article.setThemeTitle(themeTitle);

			article.setArticleTitle(articleTitle);
			article.setAuthorName(authorName);
			article.setArticleContent(articleContent);
			article.setInTime(inTime);
			articles.add(article);

			hashMap.put("article", articles);

			int startPoint = (articlePage - 1) * pagesize;
			int index = 0;

			if (startPoint == 0) {
				startPoint++;
				index++;
			}
			while (startPoint <= elements.size() - 1 && index < pagesize) {
				Element commentElement = (Element) elements.get(startPoint);
				Comment comment = new Comment();
				int commentFloor = Integer.parseInt(commentElement
						.attributeValue("commentFloor"));
				String commentAuthorName = commentElement
						.elementText("authorName");
				String comContent = commentElement.elementText("comContent");
				String commentInTime = commentElement.elementText("inTime");

				comment.setCommentFloor(commentFloor);
				comment.setAuthorName(commentAuthorName);
				comment.setComContent(comContent);
				comment.setInTime(commentInTime);

				comments.add(comment);

				startPoint++;
				index++;

			}
			hashMap.put("comments", comments);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return hashMap;
	}

	public static int getSize(String filename) {

		SAXReader saxReader = new SAXReader();

		int size = 0;

		try {
			Document document = saxReader.read(filename);
			Element root = document.getRootElement();

			List<?> elements = root.elements();

			size = elements.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (size % pagesize == 0)
			return size / pagesize;
		else
			return size / pagesize + 1;

	}
}
